﻿/*
This file is part of Ingo Karstein's SharePoint 2010 Workflow Demo project

**Do not remove this comment**

Please see the project homepage at CodePlex:
  http://spworkflowdemo.codeplex.com/

Please see my blog:
  http://ikarstein.wordpress.com

Licence: GPLv2
    SharePoint 2010 Workflow Demo
    Copyright (C) 2011  Ingo Karstein, ikarstein@hotmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

namespace ik.SharePoint2010.Workflow
{
    public partial class Task1Form : WebPartPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnSaveAsDraft.Click += new EventHandler(btnSaveAsDraft_Click);
            btnComplete.Click += new EventHandler(btnComplete_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            CloseForm();
        }

        private void CloseForm()
        {
            if( ( SPContext.Current != null ) && SPContext.Current.IsPopUI )
            {
                this.Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                this.Context.Response.Flush();
                this.Context.Response.End();
            }
            else
            {
                string str = this.Page.Request.QueryString["Source"];
                if( ( str != null ) && ( str.Length > 0 ) )
                {
                    SPUtility.Redirect(string.Empty, SPRedirectFlags.UseSource, this.Context);
                }
            }
        }

        void btnComplete_Click(object sender, EventArgs e)
        {
            SPList l = SPContext.Current.List;
            SPListItem li = SPContext.Current.ListItem;
            li[SPBuiltInFieldId.TaskStatus] = "Tasks_Completed";
            li[SPBuiltInFieldId.PercentComplete] = 1;

            SaveButton.SaveItem(SPContext.Current, false, "");

            CloseForm();
        }

        void btnSaveAsDraft_Click(object sender, EventArgs e)
        {
            SaveButton.SaveItem(SPContext.Current, false, "");

            CloseForm();
        }
    }
}
